package com.atguigu.securitydemo.security;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Component("customerUserService")
public class CustomerUserService implements UserDetailsService {
    private Map<String,String> map = new HashMap<>();
    {
        map.put("admin","{noop}admin");
        map.put("test","{noop}test");
    }
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        if(!map.containsKey(s)){
            return null;
        }
        String password = map.get(s);
        Set<SimpleGrantedAuthority> authorities = new HashSet<SimpleGrantedAuthority>();
        authorities.add(new SimpleGrantedAuthority("ROLE_WG"));
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        authorities.add(new SimpleGrantedAuthority("ROLE_ZBT"));
        authorities.add(new SimpleGrantedAuthority("ROLE_VLOG"));
        authorities.add(new SimpleGrantedAuthority("b"));
        authorities.add(new SimpleGrantedAuthority("c"));
        return new User(s,password,authorities);
    }
}
